package com.nosweattechnet.datatypes;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;

import com.nosweattechnet.server.PlaceMapAsyncTask;


public class Place implements java.io.Serializable{
	
	private static final long serialVersionUID = 1L;
	
	//Characteristics of a Place
	private String uuid;
	private String name;
	private String description;
	private String userid;
	private float longitude;
	private float latitude;
	
	//Map of the Place
	private Bitmap placemap;
	
	//Names of Columns in the database corresponding to each member variable
	private static final String placeIDString="place_id";
	private static final String placeNameString="place_name";
	private static final String placeDescripString="place_descrip";
	private static final String userIDString="user_id";
	private static final String latitudeString="place_lat";
	private static final String longitudeString="place_long";
	
	//Default values if the corresponding columns are not supplied from database
	private static final String defaultplaceIDValue="";
	private static final String defaultplaceNameValue="";
	private static final String defaultplaceDescripValue="";
	private static final String defaultuserIDValue="";
	private static final float defaultlatitudeValue=0;
	private static final float defaultlongitudeValue=0;
	
	public Place(String id, String n, String d, String uid, float lon, float lat){
		uuid=id;
		name=n;
		description=d;
		userid=uid;
		longitude=lon;
		latitude=lat;
	}
	
	//Creates Place from JSONObject returned from database
	public Place(JSONObject jobj){
		try {
			uuid=jobj.getString(Place.placeIDString);
		} catch (JSONException e) {
			uuid=Place.defaultplaceIDValue;
		}
		try {
			name=jobj.getString(Place.placeNameString);
		} catch (JSONException e) {
			name=Place.defaultplaceNameValue;
		}
		try {
			description=jobj.getString(Place.placeDescripString);
		} catch (JSONException e) {
			description=Place.defaultplaceDescripValue;
		}
		try {
			userid=jobj.getString(Place.userIDString);
		} catch (JSONException e) {
			userid=Place.defaultuserIDValue;
		}
		try {
			longitude=(float)jobj.getDouble(Place.longitudeString);
		} catch (JSONException e) {
			longitude=Place.defaultlongitudeValue;
		}
		try {
			latitude=(float)jobj.getDouble(Place.latitudeString);
		} catch (JSONException e) {
			latitude=Place.defaultlatitudeValue;
		}
	}
	
	//Getters
	public String getName(){
		return name;
	}
	
	public String getDescription(){
		return description;
	}
	
	public String getId() {
		return uuid;
	}

	public float getLongitude(){
		return this.longitude;
	}
	
	public float getLatitude(){
		return this.latitude;
	}
	
	public String getUserId(){
		return userid;
	}

	//Gets the Map of the place from the server, if it has not already been retrieved
	public Bitmap getPlaceMap(Context v){
		if(placemap==null){
			PlaceMapAsyncTask pmat=new PlaceMapAsyncTask(this.getId());
	    	pmat.execute();
	    	try {
	    		placemap = pmat.getPlaceMapbyID();
			} catch (InterruptedException e) {
			} catch (ExecutionException e) {
			}
		}
    	return placemap;
	}
	
	//Two Places are the same if and only if they have the same ID
	public boolean equals(Place p){
		return uuid==p.getId();
	}
	

}
